package cn.csxw.openchina.ui.adapter;

import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import butterknife.BindView;
import cn.csxw.openchina.R;
import cn.csxw.openchina.base.BaseRecAdapter;
import cn.csxw.openchina.entity.bean.BlogBean;
import cn.csxw.openchina.ui.activity.BlogDetailActivity;
import cn.csxw.openchina.utils.Global;

/**
 * Created by hp on 2017/1/15.
 */

public class BlogAdapter extends BaseRecAdapter<BlogBean> {

    private static ArrayList<String> blogIdList = new ArrayList<>();
    @Override
    protected void onItemClick(View v, int position) {
        BlogBean blogBean = mData.get(position);
        if(blogBean!=null){
            blogIdList.add(position+"");
            notifyDataSetChanged();
            Intent intent = new Intent(Global.mContext, BlogDetailActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("blog", blogBean);
            Global.mContext.startActivity(intent);
        }

    }

    @Override
    protected BaseViewHolder getViewHolder(View itemView) {

        return new BlogHolder(itemView);
    }

    @Override
    protected View getItemView(ViewGroup parent) {
        View itemView = Global.inflate(R.layout.layout_news_rv_item,parent);
        return itemView;
    }

    class BlogHolder extends BaseViewHolder {
        @BindView(R.id.iv_content_type)
        ImageView mIvContentType;
        @BindView(R.id.tv_news_title)
        TextView mTvNewsTitle;
        @BindView(R.id.tv_news_content)
        TextView mTvNewsContent;
        @BindView(R.id.tv_author)
        TextView mTvAuthor;
        @BindView(R.id.iv_news_time)
        ImageView mIvNewsTime;
        @BindView(R.id.tv_news_time)
        TextView mTvNewsTime;
        @BindView(R.id.iv_news_message)
        ImageView mIvNewsMessage;
        @BindView(R.id.tv_message_count)
        TextView mTvMessageCount;
        @BindView(R.id.ll_news)
        LinearLayout mLlNews;

        public BlogHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void setData(BlogBean blogBean, int position) {
            if (blogBean != null) {
                if(blogIdList.contains(position+"")){
                    mTvNewsTitle.setTextColor(Color.parseColor("#ff808080"));
                }else {
                    mTvNewsTitle.setTextColor(Color.parseColor("#ff333333"));
                }
                mTvNewsTitle.setText(blogBean.title);
                mTvNewsContent.setText(blogBean.body);
                mTvAuthor.setText(blogBean.author);
                mTvNewsTime.setText(blogBean.pubDate);
                int documentType = blogBean.documentType;
                mTvMessageCount.setText(blogBean.commentCount+"");
                if(documentType==1){
                    mIvContentType.setBackgroundResource(R.drawable.widget_original_icon);
                }else if(documentType==0){
                    mIvContentType.setBackgroundResource(R.drawable.widget_repaste_icon);
                }
                mIvContentType.setVisibility(View.VISIBLE);
            }
        }
    }
}
